package com.csun.nusing.presenter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.csun.bgaguidlib.bgaguidlib.BGABanner;
import com.csun.bgaguidlib.bgaguidlib.BGABannerUtil;
import com.csun.nusing.R;
import com.csun.nusing.presenter.inter.IBase;
import com.csun.nusing.ui.activity.LoginActivity;
import com.csun.nusing.ui.familyactivity.FamilyLoginActivity;
import com.csun.nusing.util.IntentUtils;
import com.csun.nusing.util.SPUtils;
import com.nineoldandroids.view.ViewHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 引导页面 ClassName: GuidPresenter
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-19
 */
public class GuidPresenter extends BasePresenter<IBase> {

	private boolean isFirstLoginFlag;//是否第一次登录
	private Boolean isNursingFlag;//是否是护工

	public GuidPresenter(IBase view) {
		super(view);
	}

	public void controlView(final Context context, final TextView mSkipTv,
			final Button mEnterBtn, final BGABanner mBackgroundBanner,
			final BGABanner mForegroundBanner) {

		mBackgroundBanner.setOverScrollMode(View.OVER_SCROLL_NEVER);
		mForegroundBanner.setOverScrollMode(View.OVER_SCROLL_NEVER);

		// 初始化方式1：通过传入数据模型并结合Adapter的方式初始化
		mBackgroundBanner.setAdapter(new BGABanner.Adapter() {
			@Override
			public void fillBannerItem(BGABanner banner, View view,
					Object model, int position) {
				((ImageView) view).setImageResource((Integer) model);
			}
		});

		mBackgroundBanner.setData(Arrays.asList(
				R.drawable.uoko_guide_background_1,
				R.drawable.uoko_guide_background_2,
				R.drawable.uoko_guide_background_3), null);

		// 初始化方式2：通过直接传入视图集合的方式初始化
		List<View> views = new ArrayList<View>();
		views.add(BGABannerUtil.getItemImageView(context,
				R.drawable.uoko_guide_foreground_1));
		views.add(BGABannerUtil.getItemImageView(context,
				R.drawable.uoko_guide_foreground_2));
		views.add(BGABannerUtil.getItemImageView(context,
				R.drawable.uoko_guide_foreground_3));
		mForegroundBanner.setData(views);

		/**
		 * 跳转login界面
		 */
		mSkipTv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				JumpLogin(context);
			}
		});
		/**
		 * 跳转login界面
		 */
		mEnterBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				JumpLogin(context);
			}
		});

		isFirstLoginFlag = (Boolean) SPUtils.get(context, "isFirstLoginFlag",
				false);
		isNursingFlag = (Boolean) SPUtils.get(context, "isNursingFlag",
				false);
		if (isFirstLoginFlag) {
			// 跳转登录界面
			JumpLogin(context);
		}

		// 监听页码切换事件，控制「跳过按钮」和「进入主界面的显示与隐藏」
		mForegroundBanner
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageScrolled(int position,
							float positionOffset, int positionOffsetPixels) {

						if (position == mForegroundBanner.getItemCount() - 2) {
							ViewHelper.setAlpha(mEnterBtn, positionOffset);
							ViewHelper.setAlpha(mSkipTv, 1.0f - positionOffset);

							if (positionOffset > 0.5f) {
								mEnterBtn.setVisibility(View.VISIBLE);
								mSkipTv.setVisibility(View.GONE);
							} else {
								mEnterBtn.setVisibility(View.GONE);
								mSkipTv.setVisibility(View.VISIBLE);
							}
						} else if (position == mForegroundBanner.getItemCount() - 1) {
							mSkipTv.setVisibility(View.GONE);
							mEnterBtn.setVisibility(View.VISIBLE);
							ViewHelper.setAlpha(mEnterBtn, 1.0f);
						} else {
							mSkipTv.setVisibility(View.VISIBLE);
							ViewHelper.setAlpha(mSkipTv, 1.0f);
							mEnterBtn.setVisibility(View.GONE);
						}

					}
				});
	}

	/**
	 * 跳转
	 * 
	 * @Description: TODO
	 * @param @param context
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-20
	 */
	private void JumpLogin(Context context) {
		// 第一次的时候
		if (!isFirstLoginFlag) {
			SPUtils.put(context, "isFirstLoginFlag", true);
		}
		if (isNursingFlag) {
			IntentUtils.startActivity(context, LoginActivity.class);
		}else{
			IntentUtils.startActivity(context, FamilyLoginActivity.class);
		}
		((Activity) context).finish();
		((Activity) context).overridePendingTransition(R.anim.fade_in,
				R.anim.fade_out);
	}

}
